package com.it2297.view;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import com.it2297.controller.JobController;
import com.it2297.main.App;
import com.it2297.main.UserApp;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DefaultComboBoxModel;

public class JobRemoveApplication extends JPanel {
	private JTextField tf_nric;
	private JComboBox comboBox;
	private String noResult;
	private JLabel lbl;
	private JButton btnGetAllJob;
	final JobController jct= new JobController(this);
	private JLabel lblcname;
	private JLabel lbltitle;
	private JButton btnDelete;
	private JTextField tf_jobid;
	private JLabel lblJobId;
	private JButton btnView ;

	
	public String getNRIC(){
		return tf_nric.getText();
	}
	public void setNoResult(String noResult){
		this.noResult = noResult;
	}
	public String getJobTitle(){
		return comboBox.getSelectedItem().toString();
	}
	public void setCName(String cname){
		lblcname.setText(" by the '"+ cname + "' company.");
	}
	public void setJobTitle(String title){
		lbltitle.setText("You selected '" +title + "'");
	}
	public void setJobID(String id){
		tf_jobid.setText(id);
	}
	public String getJobId(){
		return tf_jobid.getText();
	}
	/**
	 * Create the panel.
	 */
	public JobRemoveApplication() {
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Delete Job Application");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
		lblNewLabel.setBounds(10, 11, 720, 63);
		add(lblNewLabel);
		
		
		/******************HERE!!!!*****************/
		
		tf_nric = new JTextField();
		tf_nric.setEditable(false);
	//	tf_nric.setText("S9834792H");
		tf_nric.setText(UserApp.getCurrentUser().getNric());
	//	tf_nric.setText(UserApp.getCurrentNeedy().getNric());
		tf_nric.setBounds(290, 85, 264, 25);
		add(tf_nric);
		tf_nric.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Your NRIC:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_1.setBounds(188, 85, 102, 25);
		add(lblNewLabel_1);
		
		btnGetAllJob = new JButton("Get Applications");
		btnGetAllJob.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//comboBox.revalidate();
					comboBox.removeAllItems();
					comboBox.setVisible(false);
					comboBox.revalidate();
					lblcname.setText("");
					lbltitle.setText("");
					lblJobId.setText("");
					tf_jobid.setText("");
					tf_jobid.setVisible(false);
					btnDelete.setVisible(false);
					
					comboBox.addItem("---------------------------Select a job application--------------------------");
					for(int i=0; i<jct.getAllApplicationsByNeedy().size();i++){
						comboBox.addItem(jct.getAllApplicationsByNeedy().get(i));
					}
					if(noResult==""){
					JOptionPane.showMessageDialog(null, "You did not apply for any job!");
					comboBox.setVisible(false);
					btnView.setVisible(false);
					lbl.setText("");
					lblcname.setText("");
					lbltitle.setText("");
					}
					else{
						int count=comboBox.getItemCount()-1;
						lbl.setText("You applied for " + count+ " job(s):" );
					//	btnGetAllJob.setEnabled(false);
						comboBox.setVisible(true);
						btnView.setVisible(true);
					}
			}
		});
		btnGetAllJob.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnGetAllJob.setBounds(391, 121, 163, 25);
		add(btnGetAllJob);
		
		comboBox = new JComboBox();
		comboBox.setVisible(false);
		comboBox.addItem("---------------------------Select a job application--------------------------");
	/*	comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				jct.getApplicationDetails();
				tf_jobid.setVisible(true);
				lblJobId.setText("Job ID:");
				btnDelete.setVisible(true);
				if(comboBox.getSelectedItem().equals("--------------------Select a job application-------------------")){
					lblcname.setText("");
					lbltitle.setText("");
					lblJobId.setText("");
					tf_jobid.setText("");
					tf_jobid.setVisible(false);
					btnDelete.setVisible(false);
				}
				
			}
		});*/
		comboBox.setBounds(244, 157, 375, 25);
		add(comboBox);
		
		lbl = new JLabel("");
		lbl.setFont(new Font("Tahoma", Font.BOLD, 15));
		lbl.setBounds(50, 157, 184, 25);
		add(lbl);
		
		lblcname = new JLabel("");
		lblcname.setHorizontalAlignment(SwingConstants.CENTER);
		lblcname.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblcname.setBounds(10, 211, 720, 25);
		add(lblcname);
		
		lbltitle = new JLabel("");
		lbltitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		lbltitle.setHorizontalAlignment(SwingConstants.CENTER);
		lbltitle.setBounds(10, 193, 720, 25);
		add(lbltitle);
		
		btnDelete = new JButton("Delete");
		btnDelete.setVisible(false);
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int option = JOptionPane.showConfirmDialog (null, "Are you sure you want to delete the application for " + comboBox.getSelectedItem().toString() +"?",null, JOptionPane.YES_NO_OPTION);
				if (option == JOptionPane.YES_OPTION ) {
					jct.updateApplicationStatusToInactive();
					lblcname.setText("");
					lbltitle.setText("");
					lblJobId.setText("");
					tf_jobid.setText("");
					tf_jobid.setVisible(false);
					btnDelete.setVisible(false);
					comboBox.removeItem(comboBox.getSelectedItem());
					comboBox.setSelectedItem("---------------------------Select a job application--------------------------");
					JOptionPane.showMessageDialog(null, "Job application succesfully deleted!");
					int count=comboBox.getItemCount()-1;
					lbl.setText("You applied for " + count+ " job(s):" );
					
					
					if(comboBox.getItemCount()==1){
						JOptionPane.showMessageDialog(null, "You currently have no active applications!");
						comboBox.setVisible(false);
						btnView.setVisible(false);
						lbl.setText("");
						lblcname.setText("");
						lbltitle.setText("");
					}
					
					CompanySendEmail cse = new CompanySendEmail();
					cse.setVisible(true);
					cse.setTitle("Send Email");
				}
				else {
					JOptionPane.showMessageDialog(null, "Cancelled.");
				}
				
			}
		});
		btnDelete.setFont(new Font("Times New Roman", Font.BOLD, 15));
		btnDelete.setBounds(313, 269, 113, 25);
		add(btnDelete);
		
		tf_jobid = new JTextField();
		tf_jobid.setVisible(false);
		tf_jobid.setEditable(false);
		tf_jobid.setBounds(375, 238, 43, 20);
		add(tf_jobid);
		tf_jobid.setColumns(10);
		
		lblJobId = new JLabel();
		//lblJobId.setVisible(false);
		lblJobId.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblJobId.setBounds(313, 238, 102, 20);
		add(lblJobId);
		
		btnView = new JButton("View");
		btnView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				jct.getApplicationDetails();
				tf_jobid.setVisible(true);
				lblJobId.setText("Job ID:");
				btnDelete.setVisible(true);
				if(comboBox.getSelectedItem().equals("---------------------------Select a job application--------------------------")){
					lblcname.setText("");
					lbltitle.setText("");
					lblJobId.setText("");
					tf_jobid.setText("");
					tf_jobid.setVisible(false);
					btnDelete.setVisible(false);
					JOptionPane.showMessageDialog(null, "Select an application!");
				}
			}
		});
		btnView.setVisible(false);
		btnView.setFont(new Font("Times New Roman", Font.BOLD, 15));
		btnView.setBounds(629, 158, 80, 23);
		add(btnView);

	}
}
